package com.sinodata.bsm.cicp.collector.linuxmq;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.collector.Collector;
import com.sinodata.bsm.cicp.exception.CollectorException;
import com.sinodata.bsm.cicp.util.CommandReader;
import com.sinodata.bsm.common.vo.ResPropertyValue;

/**
 * 采集MQ队列
 */
public class MqQueueCollector extends Collector {
    private static Logger logger = Logger.getLogger(MqQueueCollector.class.getName());

    @Override
    public List<ResPropertyValue> collect(String ip) throws CollectorException {
        List<ResPropertyValue> values = new ArrayList<ResPropertyValue>();
        String[] propMetas = getPropMeta();
        ResPropertyValue propertyValueX = new ResPropertyValue();
        ResPropertyValue.PropValue[] propValues = new ResPropertyValue.PropValue[propMetas.length];
        BufferedReader reader = null;
        String mqName = getParamMap().get("mqName");
        String queueName = getParamMap().get("queueName");
        String cmdString = "echo \"dis q(" + queueName + ")\"  | runmqsc " + mqName;
        String maxdepthRegex = ".*\\s+MAXDEPTH\\((\\S+)\\).*";
        String maxmsglRegex = ".*\\s+MAXMSGL\\((\\S+)\\).*";
        String typeRegex = ".*\\s+TYPE\\((\\S+)\\).*";
        String putRegex = ".*\\s+PUT\\((\\S+)\\).*";
        String curdepthRegex = ".*\\s+CURDEPTH\\((\\S+)\\).*";
        String damageRegex = "AMQ8149.*";
        String maxdepth = null;
        String maxmsgl = null;
        String type = null;
        String put = null;
        String curdepth = null;
        try {
            reader = CommandReader.getReader(ip, cmdString, 0, 0);
            //reader = CollectorUtil.getReader(cmdString, ip);
            String line = null;
            Matcher match = null;
            Pattern p = null;
            while ((line = reader.readLine()) != null) {
                p = Pattern.compile(damageRegex);
                match = p.matcher(line);
                if (match.matches()) {
                    put = "damaged";
                    maxdepth = "N/A";
                    maxmsgl = "N/A";
                    type = "N/A";
                    curdepth = "N/A";
                    break;
                }
                p = Pattern.compile(maxdepthRegex);
                match = p.matcher(line);
                if (match.matches()) {
                    maxdepth = match.group(1);
                }
                p = Pattern.compile(maxmsglRegex);
                match = p.matcher(line);
                if (match.matches()) {
                    maxmsgl = match.group(1);
                }
                p = Pattern.compile(typeRegex);
                match = p.matcher(line);
                if (match.matches()) {
                    type = match.group(1);
                }
                p = Pattern.compile(putRegex);
                match = p.matcher(line);
                if (match.matches()) {
                    put = match.group(1);
                }
                p = Pattern.compile(curdepthRegex);
                match = p.matcher(line);
                if (match.matches()) {
                    curdepth = match.group(1);
                }
            }
            propValues[0] = ResPropertyValue.createPropValue(maxdepth, propMetas[0]);
            propValues[1] = ResPropertyValue.createPropValue(maxmsgl, propMetas[1]);
            propValues[2] = ResPropertyValue.createPropValue(type, propMetas[2]);
            propValues[3] = ResPropertyValue.createPropValue(put, propMetas[3]);
            propValues[4] = ResPropertyValue.createPropValue(curdepth, propMetas[4]);
            propertyValueX.setValues(propValues);
            values.add(propertyValueX);
            return values;
        } catch (Exception e) {
            logger.error(e);
            throw new CollectorException(e);
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                }
            }
        }
    }

    @Override
    public String[] getParamMeta() {
        return new String[] { "mqName", "queueName" };
    }

    @Override
    public String[] getPropMeta() {
        //        return new String[] { "MAXDEPTH", "MAXMSGL", "TYPE", "PUT", "CURDEPTH" };
        return new String[] { "$1", "$2", "$3", "$4", "$5" };
    }

}
